package com.stox.ui.javafx.charting.widget;

import java.util.Locale;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ComboBox;
import javafx.util.StringConverter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stox.dto.enumeration.Span;
import com.stox.dto.enumeration.SpanType;

@Component("spanTypeComboBox")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SpanTypeComboBox extends ComboBox<SpanType>{
	
	public SpanTypeComboBox(SpanComboBox spanComboBox){
		spanComboBox.valueProperty().addListener(new ChangeListener<Span>() {
            @Override public void changed(ObservableValue<? extends Span> ov, Span t, Span span) {
            	getItems().clear();
            	getItems().addAll(span.getSpanTypes());
            	getSelectionModel().select(0);
            }    
        });
		spanComboBox.setValue(Span.YEAR_1);
		setId("span-type-combo-box");
	}
	
	@Autowired
	public void setMessageSource(final MessageSource messageSource){
		setConverter(new StringConverter<SpanType>(){
			@Override
			public SpanType fromString(String arg0) {
				return null;
			}
			
			@Override
			public String toString(SpanType spanType) {
				return messageSource.getMessage(spanType.getCode(), null, Locale.getDefault());
			}
		});
	}
	
}
